<template>
  <div>
    <el-upload
      :action="actionUrl"
      :show-file-list="false"
      :before-upload="beforeUploadVideo"
      :on-progress="uploadVideoProcess"
      :http-request="uploadFile"
      :disabled="disableIinput"
      class="avatar-uploader el-upload--text"
      accept=".mp4">
      <video
        v-if="showVideoPath !='' && !videoFlag"
        :src="showVideoPath"
        this.global.company.showVideoPath
        class="avatar video-avatar"
        controls="controls">您的浏览器不支持视频播放</video>
      <i v-else-if="showVideoPath =='' && !videoFlag" class="el-icon-plus avatar-uploader-icon" />

      <el-progress v-if="videoFlag == true" :percentage="videoUploadPercent" type="circle" style="margin-top:30px;" />
      <!-- <el-button v-if="isShowUploadVideo" slot="trigger" class="video-btn" size="small" type="primary">选取文件</el-button> -->
    </el-upload>
    <P v-if="isShowUploadVideo" class="text">请保证视频格式正确，且不超过200M</P>
  </div>
</template>

<script>
import axios from 'axios'
import * as video from '../../../api/video.js'
export default {
  /* eslint-disable vue/require-prop-types */
  props: ['videoId', 'dialogType'],
  // 初始化参数
  data() {
    return {
      disableIinput: false,
      actionUrl: '', // 你要上传视频到你后台的地址
      videoFlag: false, // 是否显示进度条
      videoUploadPercent: '0', // 进度条的进度，
      isShowUploadVideo: false, // 显示上传按钮
      showVideoPath: ''
    }
  },

  beforeMount() {
    console.log('dialogType:', this.dialogType)
    if (this.dialogType !== 1) {
      console.log(this.videoId)
      this.getVideo(this.videoId)
    }
    if (this.dialogType === 3) {
      this.disableIinput = true
    }
  },

  created() {
    this.initUploadFileurl()
  },

  methods: {
    initUploadFileurl() {
      this.actionUrl = process.env.BASE_API + 'file/uploadFiles'
    },
    // 上传前回调
    beforeUploadVideo(file) {
      const isLt200M = file.size / 1024 / 1024 < 200
      if (['video/mp4'].indexOf(file.type) === -1) { // 'video/ogg', 'video/flv', 'video/avi', 'video/wmv', 'video/rmvb'
        this.$message.error('请上传正确的视频格式')
        return false
      }
      if (!isLt200M) {
        this.$message.error('上传视频大小不能超过200MB哦!')
        return false
      }
      this.isShowUploadVideo = false
    },
    // 进度条
    uploadVideoProcess(event, file, fileList) {
      this.videoFlag = true
      this.videoUploadPercent = file.percentage.toFixed(0) * 1
    },

    uploadFile(content) {
      console.log('内容：')
      console.log(content)
      const fd = new FormData()
      fd.append('files', content.file)

      const loading = this.$loading({
        lock: true,
        text: '保存中',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })

      axios({
        url: content.action,
        method: 'post',
        // filePath: fd,
        headers: {
          'Content-Type': 'multipart/form-data'
        }, // this is important !
        contentType: false,
        processData: false,
        // name: 'files',
        data: fd,
        params: fd

      }).then(res => {
        loading.close()

        console.log('上传成功的响应', JSON.stringify(res))
        if (res.data.code === 1) {
          this.$message({
            message: '上传成功',
            type: 'success'
          })
          console.log(res.data.data)
          this.isShowUploadVideo = false
          this.videoFlag = false
          this.videoUploadPercent = 0
          this.showVideoPath = this.HOSTURL + res.data.data[0].filePath
          this.$emit('videoSuccess', this.showVideoPath)
        } else {
          this.$message({
            message: res.data.msg,
            type: 'warning'
          })
        }
      }).catch(err => {
        console.error(err)
        this.$message.error('上传视频失败')
      })
    },
    // 显示视频
    getVideo(params) {
      video.getVideoById(params).then(res => {
        console.log('根据ID查询的视频信息：', res)
        if (res.data.code !== 1) {
          this.$message.error(res.data.msg)
          return false
        }
        if (res.data.data === 'null' || res.data.data === null) {
          return false
        }

        // this.showVideoPath = this.HOSTURL + res.data.data.records[0].video
        this.showVideoPath = res.data.data.records[0].video
      })
    }

  }

}
</script>

<style scoped="scoped">
	div {
		height: auto;
		overflow: hidden;
	}

	.avatar-uploader .el-upload {
		border: 1px dashed #d9d9d9;
		border-radius: 6px;
		cursor: pointer;
		position: relative;
		overflow: hidden;
	}

	.avatar-uploader .el-upload:hover {
		border-color: #409eff;
	}

	.avatar-uploader-icon {
		font-size: 28px;
		color: #8c939d;
		width: 178px;
		height: 178px;
		line-height: 178px;
		text-align: center;
		border: 1px solid #dcdfe6;
	}

	.avatar {
		width: 178px;
		height: 178px;
		display: block;
	}

	.video-avatar {
		width: 400px;
		height: 200px;
	}
</style>
